package service

import (
	"github.com/pkg/errors"
	"naio/config"
	"naio/model"
	"naio/util"
)

type IAdminService interface {
	AdminLoginVaidate(username string, password string) (*model.AdminModel, error)
	GetAuthInfoByID(id int) (*model.AdminModel, error)
}

type AdminService struct {
	Conf *config.BaseConfig
}

func NewAdminService(conf *config.BaseConfig) IAdminService {
	return &AdminService{Conf: conf}
}

func (s AdminService) AdminLoginVaidate(username string, password string) (*model.AdminModel, error) {
	admin, err := model.FetchAdmin(username)
	if err != nil {
		return nil, err
	}
	if util.IsEmpty(admin) {
		return nil, errors.New("用户不存在")
	}
	//检测密码是否正确
	if util.MkMd5(password) != admin.Password {
		return nil, errors.New("密码错误")
	}
	return admin, nil
}

func (s AdminService) GetAuthInfoByID(id int) (*model.AdminModel, error) {
	admin, err := model.FetchAuthInfo(id)
	if err != nil {
		return nil, err
	}
	return admin, nil
}
